package com.express.sys.controller.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;

public abstract class BaseController {

	/**
	 * 得到当前接口的参数(适用于传入的json格式)
	 * */
	public Map<String, Object> getParameter(BufferedReader reader) throws IOException {
		String line = "";
		StringBuffer inputString = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			inputString.append(line);
		}
		return ConvertJSONObjectInfo(JSONObject.parseObject(inputString.toString()));
	}
	
	public Map<String, Object> getParameter(HttpServletRequest request){
		Map<String,String[]> requestMap = request.getParameterMap();
		Map<String, Object> map = new HashMap<>();
        for(String name:requestMap.keySet()){  
            String[] values = requestMap.get(name);  
            if(values.length > 0){
            	map.put(name, values[0]);
            }else{
            	map.put(name, "");
            }
        }  
		return map;
	}
	
	/**
	 * @功能：把jsonobject转成Map
	 * */
	@SuppressWarnings("rawtypes")
	public static Map<String, Object> ConvertJSONObjectInfo(JSONObject obj) {
		Map<String, Object> map = new HashMap<>();
		if(obj != null) {
			Set<String> set = obj.keySet();
			if(set != null) {
				Iterator it = set.iterator();
				while (it.hasNext()) {
					String key = it.next().toString();
					map.put(key, obj.get(key));
				}
			}
		}
		return map;
	}
}
